package models.parsers;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import models.BankStatement;
import models.Transaction;
import org.apache.commons.lang.StringUtils;

public abstract class BaseFileParser implements Parser {
    private String _filename;

    private boolean _alreadyParsed;
    private BankStatement _statement;
    private InputStream _inputStream;

    public BaseFileParser(String filename) {
        _filename = filename;
        _alreadyParsed = false;
    }

    public BaseFileParser(InputStream inputstream) {
        _inputStream = inputstream;
    }

    public void setAlreadyParsed(boolean alreadyParsed) {
        _alreadyParsed = alreadyParsed;
    }

    public boolean alreadyParsed() {
        return _alreadyParsed;
    }

    public String getFilename() {
        return _filename;
    }

    public List<Transaction> getTransactions() {
        if (!alreadyParsed()) {
            parse();
        }
        return getStatement().transactions;
    }

    public BankStatement getStatement() {
        if (!alreadyParsed()) {
            parse();
        }

        return _statement;
    }

    public void setStatement(BankStatement statement) {
        _statement = statement;
    }

    public InputStream getInputStream() throws FileNotFoundException {

        if (!StringUtils.isEmpty(getFilename()) && _inputStream == null) {
            _inputStream = new FileInputStream(getFilename());
        }

        return _inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        _inputStream = inputStream;
    }
}
